using GoodDotNet.Core.Entity;
using GoodDotNet.Core.Repository;

using Microsoft.Extensions.Options;

namespace GoodDotNet.Core.Data
    {
    public class UnitOfWork : IUnitOfWork, IDisposable
    {

        public MyDbContext context;
        public UnitOfWork(ConnectionString conn)
        {
            context = new MyDbContext(conn);
        }

        internal bool isDipose = false;

        public void SaveChanges()
        {
            this.context.SaveChanges();
        }
        public async Task SaveChangesAsync()
        {
            await this.context.SaveChangesAsync();
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        /// <summary>
        /// 非密封类可重写的Dispose方法，方便子类继承时可重写
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (this.isDipose)
            {
                return;
            }
            //清理托管资源
            if (disposing)
            {
                //todo

            }
            context.Dispose();
            //告诉自己已经被释放
            this.isDipose = true;
        }
      
        private RoleRepository? roleRepository;
        public RoleRepository RoleRepository { get { if (roleRepository == null) roleRepository = new RoleRepository(context); return roleRepository; } }
      
          private RoleModulePermissionRepository? roleModulePermissionRepository;
        public RoleModulePermissionRepository RoleModulePermissionRepository { get { return roleModulePermissionRepository ?? (new RoleModulePermissionRepository(context)); } set { roleModulePermissionRepository = value; } }
        private UserRoleRepository? userRoleRepository;
        public UserRoleRepository UserRoleRepository { get { return userRoleRepository ?? (new UserRoleRepository(context)); } set { userRoleRepository = value; } }

        private DataBaseRepository? dataRepository;
        public DataBaseRepository DataRepository { get { return dataRepository ?? (new DataBaseRepository(context)); } set { dataRepository = value; } }

        private AdminUserRoleRepository adminUserRolleRepository;
        public AdminUserRoleRepository AdminUserRoleRepository
        {
            get
            {
                if (adminUserRolleRepository == null) adminUserRolleRepository = (new AdminUserRoleRepository(context));
                return adminUserRolleRepository;
            }
        }
        private AdminRoleModuleRepository adminRoleModuleRepository;


        public AdminRoleModuleRepository AdminRoleModuleRepository
        {
            get
            {
                if (adminRoleModuleRepository == null) adminRoleModuleRepository = (new AdminRoleModuleRepository(context));
                return adminRoleModuleRepository;
            }
        }
        private AdminModuleRepository adminModuleRepository;
        public AdminModuleRepository AdminModuleRepository
        {
            get
            {
                if (adminModuleRepository == null) adminModuleRepository = (new AdminModuleRepository(context));
                return adminModuleRepository;
            }
        }
        private AdminRoleRepository adminRoleRepository;
        public AdminRoleRepository AdminRoleRepository { get { return adminRoleRepository ?? (new AdminRoleRepository(context)); } }

    
		 private AdminUserRepository _AdminUserRepository;
		 public AdminUserRepository AdminUserRepository {
		    get
		    {
		        if (_AdminUserRepository == null) 
				    _AdminUserRepository = (new AdminUserRepository(context));
		        return _AdminUserRepository;
		    }
		}



		 private TestRepository _TestRepository;
		 public TestRepository TestRepository {
		    get
		    {
		        if (_TestRepository == null) 
				    _TestRepository = (new TestRepository(context));
		        return _TestRepository;
		    }
		}


		 private UserRepository _UserRepository;
		 public UserRepository UserRepository {
		    get
		    {
		        if (_UserRepository == null) 
				    _UserRepository = (new UserRepository(context));
		        return _UserRepository;
		    }
		}

}

}
